<script setup>
import {useRoute} from 'vue-router'
import {getOrder} from '@/api/order'
import {ref,onMounted,onBeforeUnmount} from 'vue'
const route = useRoute()
const payInfo = ref({})
const getOrderData = async ()=>{
  const res = await getOrder(route.query.id)
  console.log(res)
  payInfo.value = res.data.result
}
onMounted(async ()=>{
  await getOrderData()
  tiemFc(payInfo.value.countdown)
})
// 支付地址
const baseURL = 'http://pcapi-xiaotuxian-front-devtest.itheima.net/'
const backURL = 'http://localhost:5173/paycallback'
const redirectUrl = encodeURIComponent(backURL)
const payUrl = `${baseURL}pay/aliPay?orderId=${route.query.id}&redirect=${redirectUrl}`
let interval ;
const tiem = ref('')
const tiemFc = (date)=>{
  console.log(date)
  interval = setInterval(() => {
    date -= 1;
    if (date <= 0) {
      clearInterval(interval);
      tiem.value = '0分0秒';
    } else {
      const minutes = Math.floor(date / 60);
      const seconds = Math.floor(date  % 60);
      tiem.value = `${minutes}分${seconds}秒`;
    }
  }, 1000);
    return tiem.value
}
onBeforeUnmount(()=>clearInterval(interval))
</script>


<template>
  <div class="xtx-pay-page" v-if="payInfo.orderState === 1">
    <div class="container">
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>支付还剩 <span>{{tiem}}</span>, 超时后将取消订单</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ payInfo.payMoney?.toFixed(2) }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;"></a>
          <a class="btn alipay" :href="payUrl"></a>
        </div>
        <!-- <div class="item">
          <p>支付方式</p>
          <a class="btn" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div> -->
      </div>
    </div>
  </div>

  <div class="xtx-pay-page" v-if="payInfo.orderState === 2">
    <div class="container">
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>支付完成，商品待发货</p>
          <p>请耐心等待商家发货</p>
        </div>
         <!-- 订单信息 -->
        <div class="address">
          <p>收货人:{{ payInfo.receiverContact }}</p>
          <p >收货地址：{{ payInfo.receiverAddress }}  </p>
          <p>联系电话: {{ payInfo.receiverMobile }}</p>
        </div>
        <div class="amount">
          <span>付款总额：</span>
          <span>¥{{ payInfo.payMoney?.toFixed(2) }}</span><br/>
          <small>运费：￥{{  payInfo.postFee }}</small>
        </div>
      </div>
      <!-- 商品信息 -->
      <div class="box-goods">
        <h3 class="box-title head">商品信息</h3>
        <div class="box-body">
          <table class="goods">
            <thead>
              <tr>
                <th width="520">商品信息</th>
                <th width="170">单价</th>
                <th width="170">数量</th>
                <th width="170">小计</th>
                <th width="170">实付</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="i in payInfo.skus" :key="i.spuId">
                <td>
                  <a :href="'/goods/'+i.spuId" class="info">
                    <img :src="i.image" alt="">
                    <div class="right">
                      <p>{{ i.name }}</p>
                      <p>{{ i.attrsText }}</p>
                    </div>
                  </a>
                </td>
                <td>&yen;{{ i.curPrice }}</td>
                <td>{{ i.quantity }}</td>
                <td>&yen;{{ i.realPay }}</td>
                <td>&yen;{{ i.realPay }}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.xtx-pay-page {
  margin-top: 20px;
}
.box-goods{
  margin-top: 20px;
  background-color: #fff;
}
.box-title {
      font-size: 16px;
      font-weight: normal;
      padding-left: 10px;
      line-height: 70px;
      border-bottom: 1px solid #f5f5f5;
    }

.box-body {
      padding: 20px 0;
    }
    .goods {
  width: 100%;
  border-collapse: collapse;
  border-spacing: 0;

  .info {
    display: flex;
    text-align: left;

    img {
      width: 70px;
      height: 70px;
      margin-right: 20px;
    }

    .right {
      line-height: 24px;

      p {
        &:last-child {
          color: #999;
        }
      }
    }
  }

  tr {
    th {
      background: #FFFFFF;
      font-weight: normal;
    }

    td,
    th {
      text-align: center;
      padding: 20px;
      border-bottom: 1px solid #f5f5f5;

      &:first-child {
        border-left: 1px solid #f5f5f5;
      }

      &:last-child {
        border-right: 1px solid #f5f5f5;
      }
    }
  }
}
.pay-info {

  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;

  .icon {
    font-size: 80px;
    color: #1dc779;
  }

  .tip {
    padding-left: 10px;
    flex: 1;

    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }

      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }

  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }

      &:last-child {
        color: $priceColor;
        font-size: 20px;
      }
    }
  }
}
.address{
  margin-right: 150px;
  background-color: #fff;
  p{
    margin: 10px 0;
  }
}
.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;

  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;

    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;

    &.active,
    &:hover {
      border-color: $xtxColor;
    }

    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat center / contain;
    }

    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat center / contain;
    }
  }
}
</style>